package com.peanut.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.domain.bo.TabCouponBO;
import com.peanut.domain.bo.TabUserCouponBO;
import com.peanut.domain.po.TabCouponPO;
import com.peanut.domain.po.TabUserCouponPO;
import com.peanut.mapper.TabUserCouponMapper;
import com.peanut.service.TabCouponService;
import com.peanut.mapper.TabCouponMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 张涛
* @description 针对表【tab_coupon】的数据库操作Service实现
* @createDate 2022-05-27 10:18:45
*/
@Service
public class TabCouponServiceImpl extends ServiceImpl<TabCouponMapper, TabCouponPO>
    implements TabCouponService{

    @Resource
    private TabCouponMapper couponMapper;

    @Resource
    private TabUserCouponMapper userCouponMapper;

    @Override
    public int addCoupon(TabCouponBO bo) {
        TabCouponPO coupon = new TabCouponPO();
        BeanUtils.copyProperties(bo,coupon);
        return couponMapper.insert(coupon);
    }

    @Override
    public Page<TabCouponPO> findAll(Integer pageNumber, Integer pageSize) {
        Page<TabCouponPO> page = new Page<>(pageNumber,pageSize);
        Page<TabCouponPO> coupon = couponMapper.selectPage(page, null);
        return coupon;
    }

    @Override
    public int updateCoupon(TabCouponBO bo) {
        TabCouponPO coupon = new TabCouponPO();
        BeanUtils.copyProperties(bo,coupon);
        return couponMapper.updateById(coupon);
    }

    @Override
    public int deleteById(Long couponId) {
        return couponMapper.deleteById(couponId);
    }

    @Override
    public int getUserIdFindCoup(TabUserCouponBO userCouponBO, TabCouponBO bo) {
        TabCouponPO coupon = new TabCouponPO();
        BeanUtils.copyProperties(bo,coupon);
        int i = couponMapper.insertReturnId(coupon);
        TabUserCouponPO userCouponPO = new TabUserCouponPO();
        BeanUtils.copyProperties(userCouponBO,userCouponPO);
        userCouponPO.setCouponId(coupon.getCouponId());
        userCouponMapper.insert(userCouponPO);
        return i;
    }

    @Override
    public int deleteByUserCouponAndCoupon(Long couponId) {
        int i = couponMapper.deleteById(couponId);
        userCouponMapper.delete(new QueryWrapper<TabUserCouponPO>().eq("coupon_id",couponId));
        return i;
    }
}




